//
//  FlashCardViewController.m
//  CustomSeque
//
//  Created by Home on 2/2/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FlashCardViewController.h"
#import "FlashCards.h"
#import "DebugMacro.h"


@implementation FlashCardViewController
@synthesize swipeLeftRecognizer;
@synthesize FlashCardNumber;
@synthesize pictureImage;
@synthesize numberImage;
@synthesize numberTitle;
@synthesize soundFileURLRef, soundFileObject;


static NSString *NameKey = @"nameKey"; 
static NSString *ImageKey = @"imageKey";
static NSString *PictureKey = @"pictureKey";

UIGestureRecognizer *recognizer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        // load a different viewController
        
        
        // Can't go further than the cards in the plist
        int arraySize = [[FlashCards sharedFlashCards].contentList count];
        if (   [FlashCards sharedFlashCards].currentCardNumber >= arraySize || [FlashCards sharedFlashCards].currentCardNumber < 1 ) {
            
            DebugLog(@"HandleSwipe: Can't go beyound the number of allowed: cardNumber=%d, array size=%d",[FlashCards sharedFlashCards].currentCardNumber,arraySize);
            return;
        }

        
        [FlashCards sharedFlashCards].currentCardNumber +=1;

        

        DebugLog(@"Flashaard Swipe Left Captured %d", [FlashCards sharedFlashCards].currentCardNumber);
        
        
        FlashCardViewController *flashCard = [[FlashCardViewController alloc] init];
        [flashCard setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [self presentModalViewController:flashCard animated:YES];
        
        
        
        // [[NSBundle mainBundle] loadNibNamed:@"FlashCardViewController" owner:self options:nil];
        
    } else {
        // load an even different viewController
     
        [FlashCards sharedFlashCards].currentCardNumber -= 1;

        [self dismissModalViewControllerAnimated:YES];

        
    }
}

- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    
    DebugLog(@"Setting for cardNumber %d",[FlashCards sharedFlashCards].currentCardNumber);
    
    
    
    
    int arraySize = [[FlashCards sharedFlashCards].contentList count];
    if (   [FlashCards sharedFlashCards].currentCardNumber > arraySize || [FlashCards sharedFlashCards].currentCardNumber < 1 ) {
        
        DebugLog(@"Can't go beyound the number of allowed: cardNumber=%d, array size=%d",[FlashCards sharedFlashCards].currentCardNumber,arraySize);
        return;
    } 
    
    int cardIndex = [FlashCards sharedFlashCards].currentCardNumber - 1;

  
    FlashCardNumber.text = [NSString stringWithFormat:@"%d", [FlashCards sharedFlashCards].currentCardNumber];
    numberTitle.text = [NSString stringWithFormat:@"Page %d", [FlashCards sharedFlashCards].currentCardNumber];

    
    // Get the parameters from a singleton used to hold common data
    NSArray * contentList = [FlashCards sharedFlashCards].contentList;
    
     NSDictionary *numberItem = [contentList objectAtIndex:cardIndex];
    
    numberImage.image = [UIImage imageNamed:[numberItem valueForKey:ImageKey]];
    
    pictureImage.image = [UIImage imageNamed:[numberItem valueForKey:PictureKey]];
    
    NSURL *tapSound   = [[NSBundle mainBundle] URLForResource: [NSString stringWithFormat:@"%d",[FlashCards sharedFlashCards].currentCardNumber]
                                                withExtension: @"wav"];
	
    // Store the URL as a CFURLRef instance
    soundFileURLRef = (__bridge CFURLRef) tapSound;
	
    // Create a system sound object representing the sound file.
    AudioServicesCreateSystemSoundID (
									  
									  soundFileURLRef,
									  &soundFileObject
									  );
    
    // Comment this out if you don't what the sound to automatically play
    AudioServicesPlaySystemSound (soundFileObject);



    
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    UIGestureRecognizer *recognizer;
    // RIGHT SWIPE
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                                           action:@selector(handleSwipeFrom:)];
    [self.view addGestureRecognizer:recognizer];
    // LEFT SWIPE
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    
    self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
    
    swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    
    [self.view addGestureRecognizer:swipeLeftRecognizer];
    
    self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
    

}

- (void)viewDidUnload
{
    [self setFlashCardNumber:nil];
    [self setPictureImage:nil];
    [self setNumberImage:nil];
    [self setNumberTitle:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction) playSound: (id) sender {
	
   AudioServicesPlaySystemSound (soundFileObject);
}

- (IBAction) vibrate: (id) sender {
	
   AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
@end
